<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户昵称" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户工号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户工号" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20"
                        show-password :disabled="form.userId!=''?true:false"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{
                    dict.label
                  }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUser, getUser, updateUser} from "@/api/system/user";

export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      form: {},
      title: '新增人员',
      formLabelWidth: '80px',
      rules: {
        userName: [
          {required: true, message: "用户工号不能为空", trigger: "blur"},
          {min: 2, max: 20, message: '用户工号长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
        nickName: [
          {required: true, message: "用户昵称不能为空", trigger: "blur"}
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        password:[{
          required: true,
          message: "用户密码不能为空",
          trigger: "blur"
        }]
      },
    };
  },
  created() {

  },
  methods: {
    //获取当前行信息
    openDialog(type, row) {
      /** 修改按钮操作 */
      if (type == 'edit') {
        this.form.userId = row.userId;
        getUser(this.form.userId).then(response => {
          this.form = response.data;
          this.visible = true;
          this.title = "修改用户";
        });
      } else {
        this.form = {};
        this.form.userId = '';
      }
    },
    // 提取重置表单的逻辑
    handleClose() {
      // 通过事件通知父组件更新visible值，触发update:visible事件
      this.$emit('update:visible', false);
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      })
    },
    /** 提交按钮 */
    submit: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.visible = false;
              this.$emit('addSuccess');
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.visible = false;
              this.$emit('addSuccess');
            });
          }
        }
      });
    },
  },
};
</script>

<style></style>
